<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 模型管理
 */
class ArticleCategoryController extends CommonController {
var $formatTree;
    public function index() {
        $articleCategory = D("ArticleCategory");
        $tree = $articleCategory->getTree(0,array('status'=>array("egt",0)));
        $this->assign("tree", $tree);
        C('_SYS_GET_CATEGORY_TREE_', true); //标记系统获取分类树模板
        $this->meta_title = '分类管理';
        $this->display();
    }

    /**
     * 获取菜单
     * @param type $tree
     */
    public function tree($tree = null) {

        C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();
        $this->assign('tree', $tree);
        $this->display('tree');
    }

    /**
     * 分类添加
     */
    public function add() {

        $articleCategory = D("ArticleCategory");
        if (IS_POST) {
            if ($articleCategory->create()) {
                if ($articleCategory->add()) {
                    $this->success("添加成功", U('index'));
                } else {
                    $this->error("添加失败");
                }
            }
        }
        $pid = I("pid");
        $pid = $pid ? $pid : '0';
        $this->assign('info', array('pid' => $pid));
        $menus = D("ArticleCategory")->select();
  
        $tree = list_to_tree($menus);
        $this->formatTree = array();
        $this->_toFormatTree($tree);
       
        $this->assign("tree", $this->formatTree);
        $this->meta_title='文章分类添加';
        $this->display();
    }

    /**
     * 分类编辑
     */
    public function edit() {
        $id = I("id");
        $articleCategory = D("ArticleCategory");
        if (IS_POST) {
            if ($articleCategory->create()) {
                if ($articleCategory->save()) {
                    $this->success("编辑成功", U('index'));
                } else {
                    $this->error("编辑失败");
                }
            }
        }
         $info = $articleCategory->where(array("id"=>$id))->find();
         $this->assign("info", $info);

       $menus = $articleCategory->select();
  
        $tree = list_to_tree($menus);
        $this->formatTree = array();
        $this->_toFormatTree($tree);
       
        $this->assign("tree", $this->formatTree);
        $this->meta_title='文章分类编辑';

        $this->display("add");
    }

    /**
     * 删除分类
     */
    public function del() {
        $id=I("id");
        $this->delete("ArticleCategory", array("id" => $id));
    }

    /**
     * 选择时获取分类
     */
    public function get_class() {
        $return = array();
        $id = I("id");
        if (empty($id)) {
            return array();
        }
        $goodsclass = D("Common/GoodsClass");
        $gc_list = $goodsclass->get_goods_class_list(array("gc_parent_id" => $id));
        if (!empty($gc_list)) {
            $return['status'] = 1;
            $return['data'] = $gc_list;
            echo json_encode($return);
            exit;
        } else {
            $this->error("没有数据");
        }
    }

    //生成树形
    private function _toFormatTree($list, $level = 0, $title = 'title') {
        foreach ($list as $key => $val) {
            $tmp_str = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level * 2);
            $tmp_str.="└";

            $val['level'] = $level;
            $val['title_show'] = $level == 0 ? $val[$title] . "&nbsp;" : $tmp_str . $val[$title] . "&nbsp;";
            // $val['title_show'] = $val['id'].'|'.$level.'级|'.$val['title_show'];
            if (!array_key_exists('_child', $val)) {
                array_push($this->formatTree, $val);
            } else {
                $tmp_ary = $val['_child'];
                unset($val['_child']);
                array_push($this->formatTree, $val);
                $this->_toFormatTree($tmp_ary, $level + 1, $title); //进行下一层递归
            }
        }
        return;
    }
 /**
     * 改变名称
     */
    public function changeName($id, $v) {
        if (D("ArticleCategory")->where(array("id"=>$id))->save(array("title" => $v))) {
            return $this->success("修改成功");
        } else {
            return $this->error("修改失败");
        }
    }

    /**
     * 改变排序
     */
    public function changeSort($id, $v) {
        //判断数字  $v

        if (D("ArticleCategory")->where(array("id"=>$id))->save(array("sort" => $v))) {
            return $this->success("修改成功");
        } else {
            return $this->error("修改失败");
        }
    }
}
